<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use app\common\logic\OrderLogic;
use app\common\model\Images;
use app\common\model\OrderAftersalesRecord;
use app\common\model\ShopMember;
use think\Db;
use think\Exception;
use think\Log;
use think\Queue;
use think\Request;

/**
 * 订单管理
 *
 * @icon fa fa-user
 */
class OrderAftersales extends Backend
{

    public $OrderLogic;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->OrderLogic = new OrderLogic();
    }

    protected $relationSearch = true;
    protected $multiFields = 'status';

    protected $model = null;
    protected $agent_id = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('OrderAftersales');
    }

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model->with(['member','shop','order'])->where($where)->order($sort, $order)->paginate($limit);
            $result = array("total" => $list->total(), "rows" => $list->items());
            return json($result);
        }
        return $this->view->fetch();
    }

    public function record($ids=null)
    {
        $list = OrderAftersalesRecord::where('order_aftersales_id',$ids)->order('createtime asc')->select()->toArray();
        $this->view->assign("list", $list);
        return $this->view->fetch();
    }

    public function pass($ids=null)
    {
        Db::startTrans();
        try{
            if (empty($ids)){
                throw new Exception('参数ID缺失');
            }
            $info = \app\common\model\OrderAftersales::get(['id'=>$ids,'status'=>0]);
            if (empty($info)){
                throw new Exception('该条记录信息不存在');
            }
            OrderAftersalesRecord::create(['order_id'=>$info->order_id,'order_aftersales_id'=>$info->id,'status'=>2]);
            if ($this->OrderLogic->refund($info->order_id)){
                OrderAftersalesRecord::create(['order_id'=>$info->order_id,'order_aftersales_id'=>$info->id,'status'=>3]);
                $info->status = 3;
            }else{
                throw new Exception("订单ID：{$info->order_id}，退款失败");
//                OrderAftersalesRecord::create(['order_id'=>$info->order_id,'order_aftersales_id'=>$info->id,'status'=>4]);
//                $info->status = 4;
            }
            $order = \app\common\model\Order::get($info->order_id);
            $info->save();
            Db::commit();
            Log::info($order);
            $member_list = ShopMember::where(['shop_id'=>$order->shop_id,'is_del'=>0])->select();
            if (!empty($member_list)){
                foreach ($member_list as $v){
                    Queue::push(Message::class,[
                        'member_id'=>$v['member_id'],
                        'template_id'=>config('wechat_gzh')['gzh_template']['cancel_order'],
                        'pagepath'=>'pages/shop/order/index',
                        'data'=>[
                            'character_string1'=>$order->order_sn,
                            'time3'=>date('Y-m-d H:i')
                        ]
                    ],'message');
                }
            }
            $this->success();
        }catch (Exception $e){
            Db::rollback();
            $this->error($e->getMessage());
        }
    }

    /**
     * 拒绝
     * @param null $ids
     */
    public function refuse($ids=null)
    {
        Db::startTrans();
        try{
            if (empty($ids)){
                throw new Exception('参数ID缺失');
            }
            $info = \app\common\model\OrderAftersales::get(['id'=>$ids,'status'=>0]);
            if (empty($info)){
                throw new Exception('该条记录信息不存在');
            }
            OrderAftersalesRecord::create(['order_id'=>$info->order_id,'order_aftersales_id'=>$info->id,'status'=>1]);//添加后台审核失败记录
            \app\common\model\Order::update(['order_status'=>2],['id'=>$info['order_id']]);
            $info->status = 1;
            $info->save();
            Db::commit();
            $this->success();
        }catch (Exception $e){
            Db::rollback();
            $this->error($e->getMessage());
        }
    }


}
